package com.xul.crowd.controller;

import com.github.pagehelper.PageInfo;
import com.xul.crowd.constant.CrowdConstant;
import com.xul.crowd.entity.Admin;
import com.xul.crowd.service.AdminService;
import com.xul.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author xul
 * @create 2021-07-23 16:09
 */

@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 用户登录接口 (改用SpringSecurity  "security/do/login.html"接口)
     *
     * @param loginAcct
     * @param userPswd
     * @param session
     * @return
     */
    @RequestMapping("admin/do/login.html")
    public String doLogin(@RequestParam String loginAcct,
                          @RequestParam String userPswd,
                          HttpSession session) {
        Admin admin = adminService.getAdminLoginAcct(loginAcct, userPswd);
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN, admin);
        return "redirect:/admin/to/main/page.html";
    }

    /**
     * 用户退出接口 (改用SpringSecurity  "security/do/logout.html"接口)
     *
     * @param session
     * @return
     */
    @RequestMapping("admin/do/logout.html")
    public String doLogout(HttpSession session) {
        // session失效
        session.invalidate();
        return "redirect:/admin/to/login/page.html";
    }

    /**
     * 管理员条件查询带分页
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @param modelMap
     * @return
     */
    @RequestMapping("admin/get/page.html")
    public String getPageInfo(@RequestParam(value = "keyword", defaultValue = "") String keyword,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                              ModelMap modelMap) {
        PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);
        modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        return "admin/admin-page";
    }

    /**
     * 根据id删除管理员信息
     *
     * @param adminId
     * @param pageNum
     * @param keyword
     * @return
     */
    @RequestMapping("admin/remove/{adminId}/{pageNum}/{keyword}")
    public String remove(@PathVariable Integer adminId,
                         @PathVariable Integer pageNum,
                         @PathVariable String keyword) {
        adminService.remove(adminId);
        return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }

    /**
     * 添加管理员用户
     *
     * @param admin
     * @return
     */
    // @PreAuthorize("hasAuthority('user:save')")
    @RequestMapping("admin/save.html")
    public String save(Admin admin) {
        adminService.save(admin);
        return "redirect:/admin/get/page.html?pageNum=" + Integer.MAX_VALUE;
    }

    /**
     * 修改管理员数据 让表单回显 (带数据)
     *
     * @param adminId
     * @param modelMap
     * @return
     */
    @RequestMapping("admin/to/edit/page.html")
    public String toEditPage(@RequestParam Integer adminId,
                             ModelMap modelMap) {
        Admin admin = adminService.getAdminById(adminId);
        modelMap.addAttribute("admin", admin);
        return "admin/admin-edit";
    }

    /**
     * 表单回显后更新管理员信息
     * @param admin
     * @param pageNum
     * @param keyword
     * @return
     */
    @RequestMapping("admin/update.html")
    public String update(Admin admin,
                         @RequestParam Integer pageNum,
                         @RequestParam String keyword) {
        adminService.update(admin);
        return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword" + keyword;
    }
}
